/**
 * 
 */
package com.aepona.solution.communication.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.LinkedList;
import java.util.List;

import com.aepona.solution.communication.ConnectionThread;

/**
 * @author Dhanushka Ariyasingha.
 */
public class InternalCommunicationServer implements Runnable {

    private int port = 3014;

    private static InternalCommunicationServer serverInstance;

    public List<ConnectionThread> connectionList = null;

    private ServerSocket server;

    private InternalCommunicationServer() throws IOException {

        init();

        connectionList = new LinkedList<ConnectionThread>();

    }

    private void init() throws IOException {

        server = new ServerSocket(port);

    }

    public void start() throws IOException, InterruptedException {

        print();

        final Thread serverThread = new Thread(this);

        serverThread.setPriority(Thread.MAX_PRIORITY);

        serverThread.start();

    }

    public void run() {

        while (true) {

            try {

                final Socket socket = server.accept();

                System.out.println("Connected client to server.");

                final ConnectionThread connectionThread = new ConnectionThread(socket);
                connectionList.add(connectionThread);

            } catch (final IOException ioexception) {
                // TODO Auto-generated catch block
                ioexception.printStackTrace();
            }

        }

    }

    public void stop() {

    }

    public static InternalCommunicationServer getInstance() throws IOException {

        if (null == serverInstance) {
            serverInstance = new InternalCommunicationServer();
        }

        return serverInstance;

    }

    public void print() throws InterruptedException {

        final char[] message =
            new char[] {'A', 'e', 'p', 'o', 'n', 'a', ' ', 'I', 'n', 't', 'e', 'r', 'n', 'a', 'l', ' ', 'C', 'o', 'm', 'u', 'n', 'i', 'c', 'a', 't',
            'i', 'o', 'n', ' ', 'S', 'e', 'r', 'v', 'e', 'r', ' ', 'S', 't', 'a', 'r', 't', 'i', 'n', 'g'};

        for (final char c : message) {
            System.out.print(c);

            Thread.sleep(100);
        }

        for (int i = 0; i < 10; i++) {
            System.out.print(".");

            Thread.sleep(100);
        }

        System.out.println(" \nServer Started !!! ");
        Thread.sleep(100);
        System.out.println(" Listening to port : " + port);

    }

    /**
     * @return the connectionList
     */
    public List<ConnectionThread> getConnectionList() {

        return connectionList;
    }

    public void removeConnection(final ConnectionThread connection) {

        synchronized (connectionList) {
            connectionList.remove(connection);
        }
    }
    
    public void clear(final List<ConnectionThread> processedConnectionList) throws IOException {

        synchronized (connectionList) {

            for (final ConnectionThread connectionThread : processedConnectionList) {

                connectionThread.closeConnection();
                connectionList.remove(connectionThread);
            }
        }
        
    }

}
